<?php
namespace app\admin\model;
use think\Model;
class Result extends Model
{
	//开奖记录
	public function index(){
		$all = input('get.all');
		$key = trim(input('get.key','','htmlspecialchars'));
        if($all == 1){
        	$data = db('result')->select();
        }else{
		   $data = db('result')->where('qishu|time','like',"%".$key."%")->order('id desc')->paginate(15); 
        }
		return $data;
	}

	//下一期订单明细
	public function next(){
        //下一期开奖记录
		$next =  db('result')->field('qishu,time')->limit(1)->order('id desc')->select();
        $next['qishu']= $next['0']['qishu'];
        //买涨人数
        $next['up'] = db('order')
                    ->group('uid')
                    ->where('qishu',$next['qishu'])
                    ->where('updown',1)
                    ->count();

        //买涨金额
        $next['ups'] = db('order')
                     ->field('SUM(money) as moneys')
                     ->where('qishu',$next['qishu'])
                     ->where('updown',1)
                     ->select(); 
        if(is_null($next['ups'][0]['moneys'])){
        	$next['ups'] = 0;
        }else{
        	$next['ups'] = $next['ups'][0]['moneys'];
        }         
        //买跌人数
        $next['down'] = db('order')
                      ->group('uid')
                      ->where('qishu',$next['qishu'])
                      ->where('updown',0)
                      ->count(); 

        //买跌金额
        $next['downs'] = db('order')
                     ->field('SUM(money) as moneys')
                     ->where('qishu',$next['qishu'])
                     ->where('updown',0)
                     ->select();
        if(is_null($next['downs'][0]['moneys'])){
        	$next['downs'] = 0;
        }else{
        	$next['downs'] = $next['downs'][0]['moneys'];
        }              
        //赔率
        $set = db('set')->field('allodds')->select();             
        $next['allodds'] = $set[0]['allodds'];
        return $next;
	}
}